package com.cskaoyan.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/04/02 09:51
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
//value属性 → 包含@ConfigurationProperties注解的类 → 注册组件
public class DataSourceConfiguration {

    @Autowired
    DataSourceProperties properties;

    //有参构造方法的形参，按照类型从容器中获得组件
    /*public DataSourceConfiguration(DataSourceProperties properties) {
        this.properties = properties;
    }*/

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriver());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }
}
